#!/bin/bash

set -e

echo "------------------------------------------"  > ${5}/hilog.log
echo "pwd is $(pwd)"                              >> ${5}/hilog.log
echo "kernel src path: ${1}"                      >> ${5}/hilog.log
echo "image save path: ${2}"                      >> ${5}/hilog.log
echo "board path     : ${3}"                      >> ${5}/hilog.log
echo "product path   : ${4}"                      >> ${5}/hilog.log
echo "OHOS_HOME      : ${5}"                      >> ${5}/hilog.log
echo "device company : ${6}"                      >> ${5}/hilog.log
echo "device name    : ${7}"                      >> ${5}/hilog.log
echo "product company: ${8}"                      >> ${5}/hilog.log
echo "defconfig      : ${9}"                      >> ${5}/hilog.log
echo "kernel_build_dir: ${10} "                    >> ${5}/hilog.log
echo "kernel_build_script_dir: ${11} "             >> ${5}/hilog.log
echo "kernel_build_type: ${12} "                   >> ${5}/hilog.log
echo "enable_ramdisk : ${13}"                     >> ${5}/hilog.log
echo "------------------------------------------" >> ${5}/hilog.log

pushd ${11}

export KERNEL_SOURCE_PATH=${1} # kernel/linux/ultrarisc-ohos-kernel-v6.6, instead of kernel/linux/linux-6.6
export IMAGE_SAVE_PATH=${2} # out/dp1000/packages/phone/images
export DEVICE_BOARD_PATH=${3} # device/board/ultrarisc/dp1000
export PRODUCT_PATH="${5}/${4}" # fullpath of vendor/ultrarisc/dp1000
export OHOS_HOME=${5}
export DEVICE_COMPANY=${6} # ultrarisc
export DEVICE_NAME=${7} # dp1000
export PRODUCT_COMPANY=${8} # ultrarisc
export CONFIG_FILE_NAME=${9} # dp1000_defconfig
export BUILD_DIR=${10}
export BUILD_TYPE=${12}
export ENABLE_RAMDISK=${13} # enable_ramdisk

export LINUX_KERNEL_SRC_OUT="${BUILD_DIR}/SOURCE"
export LINUX_KERNEL_OBJ_OUT="${BUILD_DIR}/OBJ"
export KERNEL_PATCH_PATH=${OHOS_HOME}/kernel/linux/patches/linux-6.6
export LINUX_KERNEL_IMAGE_FILE=${LINUX_KERNEL_OBJ_OUT}/arch/riscv/boot/Image.gz

export OUT_DIR=${OHOS_HOME}/out # out_dir is for hdf drivers build

make -f kernel.mk

if [ -f "${LINUX_KERNEL_IMAGE_FILE}" ];then
    echo "Image: ${LINUX_KERNEL_IMAGE_FILE} build success"
else
    echo "Image: ${LINUX_KERNEL_IMAGE_FILE} build failed!!!"
    exit 1
fi

mkdir -p ${IMAGE_SAVE_PATH}
cp ${LINUX_KERNEL_IMAGE_FILE} ${IMAGE_SAVE_PATH}/Image.gz
cp ${DEVICE_BOARD_PATH}/loader/fw_payload.bin ${IMAGE_SAVE_PATH}/fw_payload.bin

# The dtb files is built in the kernel build process.
cp ${LINUX_KERNEL_OBJ_OUT}/arch/riscv/boot/dts/ultrarisc/dp1000.dtb ${IMAGE_SAVE_PATH}/dp1000.dtb
cp ${DEVICE_BOARD_PATH}/tools/burn.sh ${IMAGE_SAVE_PATH}/

popd
